package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.User;
import org.rcloud.io.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2019/1/26.
 */

@RestController
@RequestMapping(value = "/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据ID查询用户
     * @return
     */
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @ResponseBody
    @RequestMapping(value = "/get-userinfo", produces = {"application/json;charset=UTF-8"})
    public ResultVo getUser(@RequestBody String params){
        JSONObject object= JSON.parseObject(params);
        return userService.findUserFromId(object.getInteger("id"));
    }

    /**
     * 根据wx_openid查询用户
     * @return
     */
    @ApiOperation(value="获取用户详细信息", notes="根据url的wxOpenid来获取用户详细信息")
    @ApiImplicitParam(name = "wxOpenid", value = "用户openid", required = true, dataType = "String", paramType = "path")
    @ResponseBody
    @RequestMapping(value = "/find-userinfo", produces = {"application/json;charset=UTF-8"})
    public ResultVo findUser(@RequestBody String params){
        JSONObject object= JSON.parseObject(params);
        return userService.findUserFromOpenid(object.getString("wxOpenid"));
    }

    /**
     * 根据wxOpenid更改用户保存收藏
     * @return
     */
    @ApiOperation(value="保存收藏", notes="根据wxOpenid更改用户保存收藏")
    @ResponseBody
    @RequestMapping(value = "/updateinfo", produces = {"application/json;charset=UTF-8"})
    public ResultVo update(@RequestBody String params){
        String register = JSON.parseObject(params).getString("record");
        User user = JSON.parseObject(register,User.class);
        return userService.update(user);
    }


    @RequestMapping("/save")
    @ResponseBody
    @CrossOrigin
    public ResultVo insert(String nickname) {
        return userService.insert(nickname);
    }

    @RequestMapping("/like")
    @ResponseBody
    @CrossOrigin
    public PageVo<User> selectLikeRealName(String realname) {
        return userService.selectLikeRealName(realname);
    }


    /*
    *   微信端分享时调用接口获取用户的wxOpenid
    * */
    @RequestMapping(value = "/getWxOpenid", produces = {"application/json;charset=UTF-8"})
    public ResultVo getWxOpenid(@RequestBody String params){

        String result = "";
        try {
            String str = "https://api.weixin.qq.com/sns/jscode2session?appid=wx63129cb9c6591544&&secret=5f6f5e20596cd1d8d00134d32017e655&js_code=" + params;
            URL url = new URL(str);
            //得到connection对象。
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置请求方式
            connection.setRequestMethod("GET");
            //连接
            connection.connect();
            //得到响应码
            int responseCode = connection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
                //得到响应流
                InputStream inputStream = connection.getInputStream();
                //获取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null){
                    System.out.println(line);
                    result += line;
                }
                reader.close();
                //断掉链接
                connection.disconnect();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.setOK(result);
    }

}
